﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using SalePartial.Utility;
using XunPan.Web.Globals;
using SalePartial.Model;
using SalePartial.BLL;
using System.Data;
using Telerik.Web.UI;
using System.IO;

public partial class Trade_Inbox_MailBoxDetails : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            dataSource_Select();
        }

        XPNotificationInfo msg = new XPNotificationInfo();
        XPNotification xpNotifi = new XPNotification();
        msg = xpNotifi.GetXPNotification(new Guid(sdsViewDetail.SelectParameters["NotifyID"].DefaultValue));
        if (msg.CreateUserID.Equals(Session["s_userID_guid"]))
        { Button1.Visible = false; }
        else
        { Button1.Visible = true; }
       
   
    }
    private void dataSource_Select()
    {
        string NotifyID = Request.QueryString["NotifyID"];
        sdsViewDetail.SelectParameters["NotifyID"].DefaultValue = NotifyID;
    }

 protected void Button1_Click(object sender, EventArgs e)
    {
        
        if (Button1.Text == "回复")
        {
           
            
            RadEditor RadEditor1 = ((RadEditor)DetailsView1.FindControl("RadEditor1"));
            RadEditor1.Enabled = true;
            RadEditor1.Style.Clear();
            RadEditor1.CssFiles.Add("empty.css");
           // lnkAttachment.text = "";

            TextBox tb = ((TextBox)DetailsView1.FindControl("tbTitle"));
            tb.Text = "Re: " + tb.Text;
            tb.Enabled = true;
            Button1.Text = "发送";
            //HyperLink attachment = ((HyperLink)DetailsView1.FindControl("lnkAttachment"));
            RadAsyncUpload radAttachment = (RadAsyncUpload)DetailsView1.FindControl("radAttachment");
            radAttachment.Visible = true;
            Button btnSubmitAttachment=(Button)DetailsView1.FindControl("btnSubmitAttachment");
            btnSubmitAttachment.Visible = true;
            Button btnDeleteAttachment = (Button)DetailsView1.FindControl("btnDeleteAttachment");
            btnDeleteAttachment.Visible = true;
            HyperLink lnkAttachment = (HyperLink)DetailsView1.FindControl("lnkAttachment1");
            lnkAttachment.Visible = false;
        }
        else if (Button1.Text == "发送")
        {
            XPNotificationInfo msg = new XPNotificationInfo();
            XPNotification xpNotifi = new XPNotification();
            
            

            msg = xpNotifi.GetXPNotification(new Guid(sdsViewDetail.SelectParameters["NotifyID"].DefaultValue));

            if (msg.TargetID == Guid.Empty || msg.ContentType == 0)
            {
                msg.TargetID = msg.NotifyID;
            }
            else
            {
                msg.TargetID = msg.TargetID;
            }

            Guid tmpCreateID = msg.CreateUserID;
            msg.CreateUserID = msg.RecipientUserID;
            msg.RecipientUserID = tmpCreateID;
            msg.SendDate = DateTime.Now;
            msg.Status = "0100000";
            msg.NotifyID = Guid.NewGuid();
            Guid NotifyID = new Guid(sdsViewDetail.SelectParameters["NotifyID"].DefaultValue);
            //msg = xpNotifi.GetXPNotification(NotifyID);
            msg.Status = msg.Status.Remove(6, 1).Insert(6, "1");

            TextBox tb = (TextBox)DetailsView1.Rows[0].Cells[0].FindControl("tbTitle");
            msg.Title = tb.Text;
            RadEditor editor = (RadEditor)DetailsView1.Rows[0].Cells[0].FindControl("RadEditor1");
            msg.BodyText = editor.Text;
            HyperLink lnkAttachment = (HyperLink)DetailsView1.FindControl("lnkAttachment");
            msg.Attachment = lnkAttachment.NavigateUrl;
            DataSet ds = xpNotifi.SelectDynamic("TargetID='" + msg.TargetID + "'", "");
            if(ds!=null && ds.Tables.Count>0){
                msg.ContentType = ds.Tables[0].Rows.Count+1;
            }
            else{
                msg.ContentType = 1;
            }
            
            xpNotifi.Insert(msg);

            ScriptManager.RegisterStartupScript(this, GetType(), "closeWnd", "CloseWnd();", true);
        }
        else
        {
            //server code to extract and process the reply text here
            ScriptManager.RegisterStartupScript(this, GetType(), "closeWnd", "CloseWnd();", true);
        }
    }
    protected void btnSubmitAttachment_Click(object sender, EventArgs e)
    {
        RadAsyncUpload radAttachment = (RadAsyncUpload)DetailsView1.FindControl("radAttachment");
        HyperLink lnkAttachment = (HyperLink)DetailsView1.FindControl("lnkAttachment");
        Button btnSubmitAttachment = (Button)DetailsView1.FindControl("btnSubmitAttachment");
      
        Button btnDeleteAttachment = (Button)DetailsView1.FindControl("btnDeleteAttachment");
        if (radAttachment.UploadedFiles.Count > 0)
        {
            string oldfilename = radAttachment.UploadedFiles[0].GetName();
            string newfilename = null;
            string oldpath = null;
            string newpath = null;
            Trace.Warn(oldfilename);


            oldpath = Server.MapPath(radAttachment.TargetFolder) + "\\" + oldfilename;


            newfilename = FileUtility.CreateUniqueFileName(oldfilename);
            newpath = Server.MapPath(radAttachment.TargetFolder) + "\\" + newfilename;

            System.IO.File.Move(oldpath, newpath);

            lnkAttachment.Text = oldfilename;
            lnkAttachment.NavigateUrl = radAttachment.TargetFolder + "/" + newfilename;

            //将License文件的大小和类型信息存入btn中
            btnSubmitAttachment.CommandName = radAttachment.UploadedFiles[0].ContentType;
            btnSubmitAttachment.CommandArgument = radAttachment.UploadedFiles[0].ContentLength.ToString();


            lnkAttachment.Visible = btnSubmitAttachment.Visible;
            btnDeleteAttachment.Visible = btnSubmitAttachment.Visible;
            btnSubmitAttachment.Visible = !btnDeleteAttachment.Visible;
            radAttachment.Visible = !btnDeleteAttachment.Visible;
        }
    }
    protected void btnDeleteAttachment_Click(object sender, EventArgs e)
    {
        RadAsyncUpload radAttachment = (RadAsyncUpload)DetailsView1.FindControl("radAttachment");
        HyperLink lnkAttachment = (HyperLink)DetailsView1.FindControl("lnkAttachment");
        Button btnSubmitAttachment = (Button)DetailsView1.FindControl("btnSubmitAttachment");

        Button btnDeleteAttachment = (Button)DetailsView1.FindControl("btnDeleteAttachment");
        string filepath = Server.MapPath(lnkAttachment.NavigateUrl);

        System.IO.File.Delete(filepath);

        lnkAttachment.Visible = btnSubmitAttachment.Visible;
        btnDeleteAttachment.Visible = btnSubmitAttachment.Visible;
        btnSubmitAttachment.Visible = !btnDeleteAttachment.Visible;
        radAttachment.Visible = !btnDeleteAttachment.Visible;
        DetailsView1.DataBind();
    }
}